#include <limits>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;

void printStreamStatus(std::istream & is){
    cout << "is's goodbit:" << is.good() << endl;
    cout << "is's badbit:" << is.bad() << endl;
    cout << "is's failbit:" << is.fail() << endl;
    cout << "is's eofbit:" << is.eof() << endl;
}


void test0(){
    int number = 10;
    cout << "在输入之前检查cin的状态" << endl;
    printStreamStatus(cin);
    cin >> number;
    //输入的数据类型不匹配时会设为0
    cout << "number:" << number << endl;
    cout << "输入之后再检查一下cin的状态" << endl;
    printStreamStatus(cin);

    if(!cin.good()){
        //恢复流的状态
        cin.clear();
        //清空缓冲区，才能继续使用
        //从缓冲区中舍弃1024个字符，如果需要到分隔符
        //即使1024个字符，也停止舍弃
        /* cin.ignore(1024,'\n'); */
        cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
        cout << endl;
        printStreamStatus(cin);
    }

    //如果cin进入failbit状态无法再正常工作
    //不再等待这次输入
    string line;
    cin >> line;
    cout << "line:" << line << endl;


}

int main(void){
    test0();
    return 0;
}
